import React, { useState,useRef,useEffect,useMemo } from 'react';
import ProTable, { TableDropdown } from '@ant-design/pro-table';
import { Button, message, Space,Select } from 'antd';
import './css/table.css'
import Popping from './Popping';
import {useSelector,useDispatch,connect} from 'react-redux'

async function selectPage(params,url) {
    let content;
    let arr = Object.keys(params); 
    if(arr.length > 3){
        url = url + '/get'
        await fetch(url,{
            method:'post',
            headers:{
                'Content-Type':'application/json'
            },
            body:JSON.stringify(params)})
            .then(res=>res.json())
            .then(res=>{content = res})
        const result = Promise.resolve({
            data:content,
            success:true
        });
        return result;
    }else{
        await fetch(url).then(res=>res.json()).then(res=>{
            content = res;
        });
        const res = Promise.resolve(
            {
                data:content,
                success:true
            }
        )
        return res;

    }
            
}

const Table = ({title,columns,right,url,ques}) => {   
    const [columnsStateMap, setColumnsStateMap] = useState(right);
    //共享状态  
    const userList = useSelector(state=> state.user.userList)
    const checkList = useSelector(state=> state.check.checkList)
    const wordList = useSelector(state=> state.word.wordList)
    const wordhistoryList = useSelector(state=> state.wordhistory.wordhistoryList)
    const recruitList = useSelector(state=> state.recruit.recruitList)
    const scoreList = useSelector(state=> state.score.scoreList)
    const numberList = useSelector(state=> state.number.numberList)
    const knowledgeList = useSelector(state=> state.knowledge.knowledgeList)
    const policyList = useSelector(state=> state.policy.policyList)
    const chooseList = useSelector(state=> state.choose.chooseList)
    const guideList = useSelector(state=> state.guide.guideList)
    const bookList = useSelector(state=> state.book.bookList)
    const circleList = useSelector(state=> state.circle.circleList)
    const usercomsList = useSelector(state=> state.usercoms.usercomsList)
    const messageList = useSelector(state=> state.message.messageList)
    const composeList = useSelector(state=> state.compose.composeList)
    const collectList = useSelector(state=> state.collect.collectList)
    const examhistoryList = useSelector(state=> state.examhistory.examhistoryList)
    const studyhistoryList = useSelector(state=> state.studyhistory.studyhistoryList)
    const systemList = useSelector(state=> state.system.systemList)
    return (
        <ProTable columns={columns} 
        params={{sta:{userList,checkList,wordList,wordhistoryList,recruitList,scoreList,numberList,knowledgeList,policyList,chooseList,guideList,bookList,circleList,usercomsList,messageList,composeList,collectList,examhistoryList,studyhistoryList,systemList}}}
        request={(params) =>selectPage(params,url)}
        options={{}} 
        rowKey="key" columnsState={{
            value: columnsStateMap,
            onChange: setColumnsStateMap,
        }} search={
            {
                className:"form",
                labelWidth:100
            }
        } dateFormatter="string" 
        toolBarRender={() => {
            if(title == '系统管理'){
                return [<Button type="primary">无权限</Button>]
            }else if(title == '用户管理'){
                return [<Button type="primary">无权限</Button>]
            }
            else{
                return [
                    <Popping ques={ques} url={url} />
                ]
            }
        }}
        tableClassName="table"
        headerTitle={title}
        />
    )
}

export default connect()(Table)
